from datetime import date
from django.db import models
from django.contrib.auth.models import User,Group

class Product(models.Model): # 产品机种表结构
    project= models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)
    name = models.CharField(verbose_name='机种', max_length=32)
    def __str__(self) -> str:
        return self.name

class Station(models.Model): # 站点表结构
    # project = models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)
    name = models.CharField(verbose_name='站点名称', max_length=32)   # 站点名称  XRAY AOI-1 SPI-1 FLIP-1 MOUNTER GLUE VI
    cato_choices = (
        (1, '测试设备'),
        (2, '自动化设备'),
        (3, '制造设备'),
        (4, '周边'),
        (5, '资讯设备'),
    )
    catogory= models.SmallIntegerField(verbose_name='设备类型', choices=cato_choices, default=1)
    equip_no = models.CharField(verbose_name='机台编号', max_length=32) #1号机   1轨设备 2轨设备  
    def __str__(self):
        return self.name
    def get_id(self):
        return self.id

class Project(models.Model):  # 项目 线体表结构

    """ 项目表 """
    COLOR_CHOICES = (
        (1, "#56b8eb"),  # 56b8eb
        (2, "#f28033"),  # f28033
        (3, "#ebc656"),  # ebc656
        (4, "#a2d148"),  # a2d148
        (5, "#20BFA4"),  # #20BFA4
        (6, "#7461c2"),  # 7461c2,
        (7, "#20bfa3"),  # 20bfa3,
    )

    name = models.CharField(verbose_name='线体名', max_length=32)
    color = models.SmallIntegerField(verbose_name='颜色', choices=COLOR_CHOICES, default=1)
    desc = models.CharField(verbose_name='线体描述', max_length=255, null=True, blank=True)

    use_space = models.BigIntegerField(verbose_name='项目已使用空间', default=0, help_text='字节')

    star = models.BooleanField(verbose_name='星标', default=False)

    join_count = models.SmallIntegerField(verbose_name='参与人数', default=1)
    creator = models.ForeignKey(User, verbose_name='创建者', on_delete=models.CASCADE, null=True)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    members = models.ManyToManyField(User, related_name='projects', blank=True)

    # bucket = models.CharField(verbose_name='cos桶', max_length=128)
    # region = models.CharField(verbose_name='cos区域', max_length=32)

    def __str__(self):
        return self.name
    # 查询：可以省事；
    # 增加、删除、修改：无法完成
    # project_user = models.ManyToManyField(to='UserInfo',through="ProjectUser",through_fields=('project','user'))\

class ProjectDRI(models.Model): # 线体负责人员
    """ 线体负责人员 """
    project = models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)   
    manufactureruser = models.ForeignKey(User,verbose_name='线长',related_name='manufacture_DRI', on_delete=models.CASCADE,blank=True)
    manufactureruser_leader = models.ForeignKey(User,verbose_name='组长',related_name='manufacture_DRI_leader', on_delete=models.CASCADE,blank=True)
    SMTuser = models.ForeignKey(User,verbose_name='SMT DRI',related_name='SMT_DRI', on_delete=models.CASCADE,blank=True)
    SMTuser_leader = models.ForeignKey(User,verbose_name='SMT DRI_leader',related_name='SMT_DRI_leader', on_delete=models.CASCADE,blank=True)
    AOIuser = models.ForeignKey(User,verbose_name='AOI DRI',related_name='AOI_DRI', on_delete=models.CASCADE,blank=True)
    AOIuser_leader = models.ForeignKey(User,verbose_name='AOI DRI_leader',related_name='AOI_DRI_leader', on_delete=models.CASCADE,blank=True)
    PSAuser = models.ForeignKey(User,verbose_name='PSA DRI',related_name='PSA_DRI', on_delete=models.CASCADE,blank=True)
    PSAuser_leader = models.ForeignKey(User,verbose_name='PSA DRI_leader',related_name='PSA_DRI_leader', on_delete=models.CASCADE,blank=True)
    Glueuser = models.ForeignKey(User,verbose_name='点胶DRI',related_name='Gule_DRI', on_delete=models.CASCADE,blank=True)
    Glueuser_leader = models.ForeignKey(User,verbose_name='点胶DRI_leader',related_name='Gule_DRI_leader', on_delete=models.CASCADE,blank=True)
    automatic = models.ForeignKey(User,verbose_name='自动化DRI',related_name='automatic_DRI', on_delete=models.CASCADE,blank=True)
    automatic_leader = models.ForeignKey(User,verbose_name='自动化DRI_leader',related_name='automatic_DRI_leader', on_delete=models.CASCADE,blank=True)

class ProjectUser(models.Model):
    """ 项目参与者 """
    project = models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)   
    user = models.ForeignKey(User,verbose_name='参与者',related_name='paticipate', on_delete=models.CASCADE,blank=True)
    star = models.BooleanField(verbose_name='星标', default=False)

    create_datetime = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)

class ProjectStation(models.Model):  # 项目站点配置
    """ 项目站点配置"""
    project = models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)
    station = models.ForeignKey(verbose_name='站点', to='Station', on_delete=models.CASCADE)
    sequence= models.IntegerField(verbose_name='站点途程顺序')

class CommonInfo(models.Model):
    project = models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)  # 项目 此处代表线体
    product = models.ForeignKey(verbose_name='机种', to='Product', on_delete=models.CASCADE)  # 产品 此处代表机种
    DRI= models.ForeignKey(User,verbose_name='DRI',related_name='DRI', on_delete=models.CASCADE,blank=True)
    DRI_Leader= models.ForeignKey(User,verbose_name='DRI_Leader',related_name='DRI_Leader', on_delete=models.CASCADE,blank=True)
    shift_choice = (
        (1, '白'),
        (2, '夜'),
   
    )
    shift = models.SmallIntegerField(verbose_name='白夜班', choices=shift_choice, default=1)
    report_date=models.DateField(verbose_name='日期',default=date.today)
    class Meta:
        abstract = True

class Wiki(CommonInfo): # 交接
    title = models.CharField(verbose_name='标题', max_length=32)
    content = models.TextField(verbose_name='交接内容')
    deal_time= models.IntegerField(verbose_name='处理时间min')

    depth = models.IntegerField(verbose_name='深度', default=1)
    priority_choices = (
        ("danger", "高"),
        ("warning", "中"),
        ("success", "低"),
        ("no_issue", "无"),
    )
    priority = models.CharField(verbose_name='优先级', max_length=12, choices=priority_choices, default='no_issue')
    # 子关联
    parent = models.ForeignKey(verbose_name='父文章', to="Wiki", null=True, blank=True, related_name='children', on_delete=models.CASCADE)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return self.title

class Issues(models.Model):
    """ 问题 """
    project = models.ForeignKey(verbose_name='项目', to='Project', on_delete=models.CASCADE)
    issues_type = models.ForeignKey(verbose_name='问题类型', to='IssuesType',on_delete=models.CASCADE)
    module = models.ForeignKey(verbose_name='模块', to='Module', null=True, blank=True, on_delete=models.CASCADE)

    subject = models.CharField(verbose_name='主题', max_length=80)
    desc = models.TextField(verbose_name='问题描述')
    priority_choices = (
        ("danger", "高"),
        ("warning", "中"),
        ("success", "低"),
    )
    priority = models.CharField(verbose_name='优先级', max_length=12, choices=priority_choices, default='danger')

    # 新建、处理中、已解决、已忽略、待反馈、已关闭、重新打开
    status_choices = (
        (1, '新建'),
        (2, '处理中'),
        (3, '已解决'),
        (4, '已忽略'),
        (5, '待反馈'),
        (6, '已关闭'),
        (7, '重新打开'),
    )
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choices, default=1)

    assign = models.ForeignKey(User,verbose_name='指派', related_name='task', null=True, blank=True, on_delete=models.SET_NULL)
    participant = models.ManyToManyField(User,verbose_name='关注者', related_name='observe', blank=True)

    start_date = models.DateField(verbose_name='开始时间', null=True, blank=True)
    end_date = models.DateField(verbose_name='结束时间', null=True, blank=True)
    mode_choices = (
        (1, '公开模式'),
        (2, '隐私模式'),
    )
    mode = models.SmallIntegerField(verbose_name='模式', choices=mode_choices, default=1)

    parent = models.ForeignKey(verbose_name='父问题', to='self', related_name='child', null=True, blank=True,
                               on_delete=models.SET_NULL)

    creator = models.ForeignKey(User,verbose_name='创建者',  related_name='create_problems',on_delete=models.SET_NULL, null=True, blank=True)

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    latest_update_datetime = models.DateTimeField(verbose_name='最后更新时间', auto_now=True)

    def __str__(self):
        return self.subject

class Module(models.Model):
    """ 模块（里程碑,项目阶段EVT,DVT,NPI）"""
    project = models.ForeignKey(verbose_name='项目', to='Project',on_delete=models.CASCADE)
    title = models.CharField(verbose_name='模块名称', max_length=32)

    def __str__(self):
        return self.title

class IssuesType(models.Model):
    """ 问题类型 例如：任务、功能、Bug """

    PROJECT_INIT_LIST = ["任务", '功能', '维修']

    title = models.CharField(verbose_name='类型名称', max_length=32)
    project = models.ForeignKey(verbose_name='项目', to='Project', on_delete=models.CASCADE)

    def __str__(self):
        return self.title

class FileRepository(models.Model):
    """ 文件库 """
    project = models.ForeignKey(verbose_name='项目', to='Project',on_delete=models.CASCADE)
    file_type_choices = (
        (1, '文件'),
        (2, '文件夹')
    )
    file_type = models.SmallIntegerField(verbose_name='类型', choices=file_type_choices)
    name = models.CharField(verbose_name='文件夹名称', max_length=32, help_text="文件/文件夹名")
    key = models.CharField(verbose_name='文件储存在COS中的KEY', max_length=128, null=True, blank=True)

    # int类型最大表示的数据
    file_size = models.BigIntegerField(verbose_name='文件大小', null=True, blank=True, help_text='字节')

    file_path = models.CharField(verbose_name='文件路径', max_length=255, null=True,
                                 blank=True)  # https://桶.cos.ap-chengdu/....

    parent = models.ForeignKey(verbose_name='父级目录', to='self', related_name='child', null=True, blank=True,on_delete=models.CASCADE)

    update_user = models.ForeignKey(User,verbose_name='最近更新者',on_delete=models.CASCADE)
    update_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)

class project_person(models.Model):
    project = models.ForeignKey(verbose_name='线体', to='Project', on_delete=models.CASCADE)
    product = models.ForeignKey(verbose_name='机种', to='Product',on_delete=models.CASCADE)
    ops_person = models.ForeignKey(User,verbose_name='线组长',related_name='ops_person' ,on_delete=models.CASCADE)
    aoi_person = models.ForeignKey(User,verbose_name='AOI',related_name='aoi_person',on_delete=models.CASCADE)
    automation_person = models.ForeignKey(User,verbose_name='自动化',related_name='automation_person',on_delete=models.CASCADE)
    dfx_person = models.ForeignKey(User,verbose_name='DFX',related_name='dfx_person',on_delete=models.CASCADE)
    pe_person = models.ForeignKey(User,verbose_name='PE',related_name='pe_person',on_delete=models.CASCADE)
    qa_person = models.ForeignKey(User,verbose_name='QA',related_name='qa_person',on_delete=models.CASCADE)
    mes_person = models.ForeignKey(User,verbose_name='MES',related_name='mes_person',on_delete=models.CASCADE)

class share_items(models.Model):
    TOP_analysis1= models.TextField(verbose_name='TOP项分析1',null=True,blank=True)
    cause_solution1 = models.TextField(verbose_name='原因及措施1',null=True,blank=True)
    TOP_analysis2= models.TextField(verbose_name='TOP项分析2',null=True,blank=True)
    cause_solution2 = models.TextField(verbose_name='原因及措施2',null=True,blank=True)
    class Meta:
        abstract = True

class FPY_rate(CommonInfo,share_items):
    spi_1 = models.FloatField(verbose_name='SPI-1直通率',null=True,blank=True)
    post_aoi_1 = models.FloatField(verbose_name='POST-AOI-1直通率',null=True,blank=True)
    xray_1 = models.FloatField(verbose_name='XRAY-1直通率',null=True,blank=True)
    spi_2 = models.FloatField(verbose_name='SPI-2直通率',null=True,blank=True)
    post_aoi_2 = models.FloatField(verbose_name='POST-AOI-2直通率',null=True,blank=True)
    xray_2 = models.FloatField(verbose_name='XRAY-2直通率',null=True,blank=True)
    DRI= models.ForeignKey(User,verbose_name='DRI',related_name='fpy_DRI', on_delete=models.CASCADE,blank=True)
    DRI_Leader= models.ForeignKey(User,verbose_name='DRI_Leader',related_name='fpy_DRI_Leader', on_delete=models.CASCADE,blank=True)

class Yield_rate(CommonInfo,share_items):
    spi_1 = models.FloatField(verbose_name='SPI-1良率',null=True,blank=True)
    spi_2 = models.FloatField(verbose_name='SPI-2良率',null=True,blank=True)
    pre_aoi_1 = models.FloatField(verbose_name='PRE-AOI-1良率',null=True,blank=True)
    pre_aoi_2 = models.FloatField(verbose_name='PRE-AOI-2良率',null=True,blank=True)
    post_aoi_1 = models.FloatField(verbose_name='POST-AOI-1良率',null=True,blank=True)
    post_aoi_2 = models.FloatField(verbose_name='POST-AOI-2良率',null=True,blank=True)
    DRI= models.ForeignKey(User,verbose_name='DRI',related_name='yield_DRI', on_delete=models.CASCADE,blank=True)
    DRI_Leader= models.ForeignKey(User,verbose_name='DRI_Leader',related_name='yield_DRI_Leader', on_delete=models.CASCADE,blank=True)

class Output_by2hour(CommonInfo):
    achieve1=models.IntegerField(verbose_name='时段8:00-10:00目标',null=True,blank=True)
    achieve2=models.IntegerField(verbose_name='时段10:00-13:00目标',null=True,blank=True)
    achieve3=models.IntegerField(verbose_name='时段13:00-15:00目标',null=True,blank=True)
    achieve4=models.IntegerField(verbose_name='时段15:00-17:00目标',null=True,blank=True)
    achieve5=models.IntegerField(verbose_name='时段17:00-20:00目标',null=True,blank=True)
    target1=models.IntegerField(verbose_name='时段8:00-10:00目标',null=True,blank=True)
    target2=models.IntegerField(verbose_name='时段10:00-13:00目标',null=True,blank=True)
    target3=models.IntegerField(verbose_name='时段13:00-15:00目标',null=True,blank=True)
    target4=models.IntegerField(verbose_name='时段15:00-17:00目标',null=True,blank=True)
    target5=models.IntegerField(verbose_name='时段17:00-20:00目标',null=True,blank=True)
    DRI= models.ForeignKey(User,verbose_name='DRI',related_name='output_DRI', on_delete=models.CASCADE,blank=True)
    DRI_Leader= models.ForeignKey(User,verbose_name='DRI_Leader',related_name='output_DRI_Leader', on_delete=models.CASCADE,blank=True)


